<?
class Widget_model extends CI_Model
{	
	function __construct()
	{
		parent::__construct();
		
	}
	private function _required($required, $data)
	{
		foreach($required as $field) if(!isset($data[$field])) return false;
		return true;
	}
	
	private function _default($defaults, $options)
	{
		return array_merge($defaults, $options);
	}
	
	function insert($options = array())
	{
		//if(!$this->_required(array(.$qualification_fields.), $options)) 
		//		return false;
	
		//$options = $this->_default(array('u_created_on' => sql_current_datetime()), $options);
		if($options['data'])
			$options['data'] = json_encode($options['data']);
		
		$qualificationArray = array('id', 'name', 'file_name', 'data', 'owner');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);
	 
		$this->db->insert('widgets');
	
		return $this->db->insert_id();
	}
	
	function update($options = array())
	{
		if(!$this->_required(array('id'), $options)) 
			return false;

		if($options['data'])
			$options['data'] = json_encode($options['data']);
			
		$qualificationArray = array('id', 'name', 'file_name', 'data', 'owner');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->set($qualifier, $options[$qualifier]);	
		
		$this->db->where('id', $options [ 'id' ]);	
		$this->db->update('widgets');	
		return $this->db->affected_rows();
	}
	
	function get($options = array())
	{
		$options = $this->_default(array('sort_direction' => 'asc'), $options);
	
		$qualificationArray = array('id', 'name', 'data', 'file_name', 'owner');
		foreach($qualificationArray as $qualifier)
			if(isset($options[$qualifier])) 
				$this->db->where($qualifier, $options[$qualifier]);
	
		if(isset($options ['limit']) && isset($options ['offset'])) 
			$this->db->limit($options ['limit'], $options ['offset']);
		else if(isset($options ['limit'])) 
			$this->db->limit($options ['limit']);
	
		if(isset($options ['sort_by'])) 
		$this->db->order_by($options ['sort_by'], $options ['sort_direction']);
		
		$query = $this->db->get('widgets');
		
		if($query->num_rows() == 0) 
			return false;
	
		if(isset($options ['id']))
		{
			$row = $query->row_array(0);
			$row['data'] = json_decode($row['data'], true);
			return $row;
		}
		else
		{
			$rows = $query->result_array();
			for($i = 0; $i < count($rows); $i++)
			{
				$rows[$i]['data'] = json_decode($rows[$i]['data'], true);
			}
			return $rows;
		}
	}
	
	function delete($options = array())
	{
		if(!$this->_required(array('id'), $options)) 
			return false;
	
		$this->db->where('id', $options ['id']);
		$this->db->delete('widgets');
     
	}

	function install($file_name, $name)
	{
		$file_name = strtolower($file_name);
		include(APPPATH . 'widgets/' . $file_name . '.php');
		$file_name = ucfirst($file_name);
		
		$widget = new $file_name();
		$defaults = $widget->defaults();
		
        $file_name = strtolower($file_name);
		$exists = $this->get(array('name' => $name));
		if($exists === FALSE)
			return $this->insert(array('name' => $name, 'file_name' => $file_name, 'data' => $defaults));
		else
			return FALSE;
	}
	function uninstall($id)
	{
		return $this->delete(array('id' => $id));
	}
	function get_available_widgets()
	{
		$this->load->helper('directory');
		$widgets = array();
		foreach(directory_map(APPPATH . '/widgets') as $widget)
		{
			$widgets[] = str_replace('.php', '', $widget);
		}
		return $widgets;
	}

	
	function get_installed_widgets()
	{
		return $this->get();
	}
	
}